/////////////////////////////////////////////////////////////////////
//  Package Description:
//    BBC iPlayer plugin for MythTV by Grant French (c) 2008-2009
//
//  Filename:  iplayerupdate.cpp
//  Author:    Grant French
//  Date:      06/12/2008
//
//  Description:
//    Empties the iplayer programme list table and rebuilds it
//      by requesting the XMLParser to download and commit all
//      the BBC iPlayer A-Z feeds (TV Only), and show progress.
//
/////////////////////////////////////////////////////////////////////


#include <qapplication.h>
#include <qstringlist.h>
#include <qpainter.h>

#include <mythtv/mythdbcon.h>
#include <mythtv/mythcontext.h>

#include "globals.h"
#include "iplayerupdate.h"
#include "xmlparser.h"

iPlayerUpdate::iPlayerUpdate() :
    MythProgressDialog("Updating from BBC iPlayer feeds...", 100)
{

    double counter = 0.0;
    char* feedUrl = "http://feeds.bbc.co.uk/iplayer/atoz/%s/list/tv";
    char buff[255];
    QStringList urls;

    //TODO: loop to create these a-z (this was just quicker to test at the moment)
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/a/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/b/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/c/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/d/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/e/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/f/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/g/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/h/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/i/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/j/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/k/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/l/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/m/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/n/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/o/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/p/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/q/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/r/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/s/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/t/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/u/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/v/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/w/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/x/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/y/list/tv";
    urls += "http://feeds.bbc.co.uk/iplayer/atoz/z/list/tv";

  //Calculate percentage points per url processed
  double pointVal = 100 / urls.count();

  //Start a fresh, wipe previous data
  MSqlQuery query(MSqlQuery::InitCon());
  query.prepare("TRUNCATE iplayer_metadata");
  query.exec();


  //Go through each URL
  for (QStringList::const_iterator iter = urls.begin(); iter != urls.end();
        ++iter)
  {

      //Get our XMLParser to download, process and commit this url
      new XMLParser( *iter );

      //Keep track of progress
      counter += pointVal;
      if (counter > 100) counter=100;
      setProgress( int (counter) );
  }

  close();
  deleteLater();


}

iPlayerUpdate::~iPlayerUpdate()
{
}

// void iPlayerUpdate::ExitWin()
// {
//         emit accept();
// }
